﻿using RRQMMVVM;
using System.Threading;
using System.Windows;
using ViewModel.WindowDialog;

namespace ViewModel.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel(Window window)
        {
            Instance = this;
            Window = window;

            this.PersonalSettingCommand = new ExecuteCommand(PersonalSetting);
            this.MoreCommand = new ExecuteCommand(More);
            this.AddFriendsCommand = new ExecuteCommand(AddFriends);

            this.WindowTitleViewModel = new WindowTitleViewModel(window);
            this.WindowBody = new WindowBodyViewModel();
            this.WindowDialog = new WindowDialogViewModel();
        }

        #region Command

        public ExecuteCommand PersonalSettingCommand { get; set; }
        public ExecuteCommand MoreCommand { get; set; }
        public ExecuteCommand AddFriendsCommand { get; set; }

        #endregion Command

        #region 属性

        /// <summary>
        /// 唯一实例
        /// </summary>
        public static MainViewModel Instance { get; set; }

        /// <summary>
        /// 绑定的目标窗口
        /// </summary>
        public static Window Window { get; set; }

        private bool popupIsOpen;

        public bool PopupIsOpen
        {
            get { return popupIsOpen; }
            set { popupIsOpen = value; OnPropertyChanged(); }
        }

        private WindowBodyViewModel windowBody;

        public WindowBodyViewModel WindowBody
        {
            get { return windowBody; }
            set { windowBody = value; OnPropertyChanged(); }
        }

        private WindowDialogViewModel windowDialog;

        public WindowDialogViewModel WindowDialog
        {
            get { return windowDialog; }
            set { windowDialog = value; OnPropertyChanged(); }
        }

        public WindowTitleViewModel WindowTitleViewModel { get; set; }

        #endregion 属性



        #region 公共方法

        public void GetMyInfo()
        {
            Thread thread = new Thread(UiTimer);
            thread.IsBackground = true;
            thread.Start();

            //Userinfo userinfo = null;
            //while (userinfo == null)
            //{
            //    userinfo = TransferClient.GetSelfinfo();
            //}

            //if (userinfo == null || !userinfo.Status)
            //{
            //    return;
            //}
            //Window.Dispatcher.Invoke(() =>
            //{
            //    this.WindowDialog.Nickname = userinfo.Nickname;
            //});
        }

        private void UiTimer()
        {
            //Stopwatch stopwatch = new Stopwatch();
            //while (true)
            //{
            //    stopwatch.Start();
            //    FriendList friendList = TransferClient.GetFriendList();

            //    Window.Dispatcher.Invoke(() =>
            //    {
            //        if (friendList != null && friendList.Status)
            //        {
            //            if (friendList.FriendListCollection.Count == this.WindowBody.FriendListItems.Count)
            //            {
            //                for (int i = 0; i < friendList.FriendListCollection.Count; i++)
            //                {
            //                    this.WindowBody.FriendListItems[i].Account = friendList.FriendListCollection[i].SourceAccount;
            //                    if (this.WindowBody.FriendListItems[i].NickName != friendList.FriendListCollection[i].Nickname)
            //                    {
            //                        this.WindowBody.FriendListItems[i].NickName = friendList.FriendListCollection[i].Nickname;
            //                        foreach (var item in MainViewModel.Instance.WindowBody.RecentlyContactListItems)
            //                        {
            //                            if (item.Account== this.WindowBody.FriendListItems[i].Account)
            //                            {
            //                                item.NickName = this.WindowBody.FriendListItems[i].NickName;
            //                                break;
            //                            }
            //                        }
            //                    }
            //                }

            //            }
            //            else
            //            {
            //                this.WindowBody.FriendListItems.Clear();
            //                foreach (var item in friendList.FriendListCollection)
            //                {
            //                    ContactItemViewModel contactItem = new ContactItemViewModel();
            //                    contactItem.Account = item.SourceAccount;
            //                    contactItem.NickName = item.Nickname;
            //                    this.WindowBody.FriendListItems.Add(contactItem);
            //                }
            //                this.WindowBody.ContactText = this.WindowBody.ContactText;
            //            }

            //        }
            //    });

            //    stopwatch.Stop();
            //    Thread.Sleep(2000);
            //    Console.WriteLine(stopwatch.Elapsed.ToString());
            //    stopwatch.Restart();
            //}
        }

        #endregion 公共方法

        private void PersonalSetting()
        {
            this.WindowDialog.TempNickname = this.WindowDialog.Nickname;
            this.WindowDialog.DialogVisible = Visibility.Visible;
            this.WindowDialog.PersonalSettingVisible = Visibility.Visible;
        }

        private void More()
        {
            this.PopupIsOpen = !this.PopupIsOpen;
        }

        private void AddFriends()
        {
            this.PopupIsOpen = false;
            this.WindowDialog.SearchFriendMessage = string.Empty;
            this.WindowDialog.DialogVisible = Visibility.Visible;
            this.WindowDialog.SearchFriendVisible = Visibility.Visible;
        }
    }
}